var skydns = {
  onLoad: function() {
    // initialization code
	this.panel = document.getElementById("skydns-toolbar-popup");
	this.browser = document.getElementById("skydns-browser");
	this.button = document.getElementById("skydns-toolbar-button");
	(function(loader, context){
	  loader.loadSubScript("chrome://skydns/content/jquery-1.5.1.min.js", context);
	  loader.loadSubScript("chrome://skydns/content/jquery.uuid.js", context);
	  //loader.loadSubScript("chrome://skydns/content/framework.js", context);
	  loader.loadSubScript("chrome://skydns/content/skydns.js", context);
	})(
	  Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(
		  Components.interfaces.mozIJSSubScriptLoader
	  ),  skydns );
		
		this.panel.addEventListener('popupshown', function(){
			skydns.browser.setAttribute('src', 'chrome://skydns/content/popup.html');
			skydns.browser.addEventListener('DOMContentLoaded', function(){
				//skydns.browser.contentDocument.addEventListener('DOMSubtreeModified', skydns.onBrowserResize, true);
				var popupWnd = new XPCNativeWrapper(skydns.browser.contentWindow).wrappedJSObject;
				popupWnd.bgWindow = skydns;
				//skydns.onBrowserResize.call(skydns.browser.contentDocument);
			}, true);		
		}, false);
		
		this.panel.addEventListener('popuphide', function(){
			var popupWnd = new XPCNativeWrapper(skydns.browser.contentWindow).wrappedJSObject,
			evt = document.createEvent("HTMLEvents");
			evt.initEvent('unload', true, true ); // event type,bubbling,cancelable
			popupWnd.dispatchEvent(evt);
			//skydns.browser.contentDocument.removeEventListener('DOMSubtreeModified', skydns.onBrowserResize, true);
			//skydns.browser.setAttribute('src', '');
		}, false);
  },
  
  onToolbarButtonCommand: function(e) {
		this.panel.openPopup(this.button, 'bottomcenter topright', 0, 0, false, false);
  },
  
  onBrowserResize: function(e){
	//$(skydns.panel).width($(this.body).width()+28).height($(this.body).height()+28);
  }
};

window.addEventListener("load", function () { skydns.onLoad(); }, false);
